package com.woniuxy.shop.controller;


import com.woniuxy.shop.entity.User;
import com.woniuxy.shop.service.OrderService;
import com.woniuxy.shop.service.UserService;
import com.woniuxy.shop.utils.CookieUtils;
import com.woniuxy.shop.utils.JwtUtils;
import com.woniuxy.shop.vo.CartItemVo;
import com.woniuxy.shop.vo.OrderPreViewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fanqiwen
 * @since 2023-02-07 11:56:02
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;
    //订单预览界面
    @RequestMapping("orderCartItem")
    public OrderPreViewVo orderCartItem(@RequestParam("productIds[]" )Integer[]productIds, HttpServletRequest request){
        User currentUser = getCurrentUser(request);
       OrderPreViewVo orderPreViewVo=orderService.queryOrderPreViewVo(currentUser.getId(),productIds);
        return orderPreViewVo;
    }
    @RequestMapping("addOrder")
    public String addOrder(@RequestParam("productIds[]" )Integer[]productIds,Integer selectAddressId, HttpServletRequest request){
        User currentUser = getCurrentUser(request);
        orderService.addOrder(productIds,currentUser.getId(),selectAddressId);
        return "ok";
    }



    private User getCurrentUser(HttpServletRequest request) {
        String token = CookieUtils.getUserToken2Cookie(request);
        String account = JwtUtils.getAccount(token);
        return userService.getUserByName(account);
    }

}

